using System;
using System.Drawing;
using System.Globalization;
namespace CUtil
{
	/// <summary>
	/// Summary description for MathUtil.
	/// </summary>
	public class MathUtil
	{
		#region Constructors and Destructors
       

		public MathUtil()
		{
            
            //DisplayDateNCultureName(tDate, "en-US");

		}

		#endregion

		#region Methods

		/// <summary>
		/// GetValueLimitCalculator
		/// </summary>
		/// <returns></returns>
		public ValueLimitCalculator GetValueLimitCalculator()
		{
			return new ValueLimitCalculator();
		}
		/// <summary>
		/// GetValueLimitCalculator
		/// </summary>
		/// <returns></returns>
		public ValueLimitCalculator GetValueLimitCalculator(PointF[] doubleextPoints)
		{
			return new ValueLimitCalculator(doubleextPoints);
		}
        public static double TruncateFloatWithDigits(double fIn, int numberOfDigits)
        {
            if (fIn == 0)
                return fIn;
            string fSplit = fIn.ToString(Conversions.CultureAmerican);

            try
            {


                string[] arrSplit = fSplit.Split(new char[] { '.' }, 100);
                if (arrSplit.GetLength(0) < 2)
                    return fIn;

                string afterComma = arrSplit[1];
                if (afterComma.Length > numberOfDigits)
                    afterComma = afterComma.Remove(numberOfDigits);
                string newNumberString = arrSplit[0] + "." + afterComma;

                double fOut = Convert.ToDouble(newNumberString, Conversions.CultureAmerican);
                return fOut;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Assert(false, "Error truncating value " + fSplit + "; " + ex.Message);
                return fIn;
            }

        }
        public static string FormatStringWithTwoDigits(double fIn)
        {
            string s = fIn.ToString("N");
            return s;
        }
        public static double FormatFloatWithTwoDigits(double fIn)
        {

            string s = fIn.ToString("N");
            fIn = Convert.ToDouble(s);
            return fIn;
        }
        public static double FormatDoubleWithTwoDigits(double dIn)
        {
            
            string s = dIn.ToString("N");
            dIn = Convert.ToDouble(s);
            return dIn;
        }

		#endregion

		#region Class ValueLimitCalculator

		/// <summary>
		/// Summary description for ValueLimits.
		/// </summary>
		public class ValueLimitCalculator
		{
			#region Fields

			public double XMin=0.0f;
			public double XMax=0.0f;
			public double YMin=0.0f;
			public double YMax=0.0f;

			#endregion

			#region Constructor
			/// <summary>
			/// ValueLimits
			/// </summary>
			public ValueLimitCalculator()
			{
			}
			/// <summary>
			/// ValueLimits
			/// </summary>
			/// <param name="doubleextPoints"></param>
			public ValueLimitCalculator(PointF[] doubleextPoints)
			{
				this.CalculateFloatValueLimits(doubleextPoints);
			}
			#endregion

			#region Methods Private

			/// <summary>
			/// CalculateFloatValueLimits
			/// </summary>
			/// <param name="doubleextPoints"></param>
			private void CalculateFloatValueLimits(PointF[] doubleextPoints)
			{
				int pointCount=doubleextPoints.Length;
				double fPointXMax=double.MinValue;
				double fPointYMax=double.MinValue;
				double fPointXMin=double.MaxValue;
				double fPointYMin=double.MaxValue;
				for (int i=0;i<pointCount;i++)
				{
					double fPointX=doubleextPoints[i].X;
					double fPointY=doubleextPoints[i].Y;
					if (fPointXMin>fPointX) {fPointXMin=fPointX;}
					if (fPointYMin>fPointY) {fPointYMin=fPointY;}
					if (fPointXMax<fPointX) {fPointXMax=fPointX;}
					if (fPointYMax<fPointY) {fPointYMax=fPointY;}
				}
				this.XMin=fPointXMin;
				this.XMax=fPointXMax;
				this.YMin=fPointYMin;
				this.YMax=fPointYMax;
			}
			#endregion

			#region Methods
			/// <summary>
			/// GetRangeX
			/// </summary>
			/// <returns></returns>
			public double GetRangeX()
			{
				return this.XMax-this.XMin;
			}
			/// <summary>
			/// GetRangeY
			/// </summary>
			/// <returns></returns>
			public double GetRangeY()
			{
				return this.YMax-this.YMin;
			}
			#endregion
		}

		#endregion
	}
}
